<#include "/@inc/funcMacros.ftl">
<#assign className = TriaGram + "EA" + Prefix >
<#assign baseName = TriaGram + Prefix + LateType>
<#assign IDLName = TriaGram + "IA" + Prefix + LateType>
<#assign intfName = TriaGram + "I" + Prefix>
<#assign IDLFactory = TriaGram + "IA" + Prefix + "Factory">
<@pp.renameOutputFile name=className+"Factory.cpp">
</@pp.renameOutputFile>
<#include "/@inc/copyright.txt">
//
// ${className}Factory.cpp
// Provide implementation to interface
//      ${IDLFactory}
//===============================================================================
//  ${Month} ${Year}  Creation: Code generated by the CAA wizard  ${Author}
//===============================================================================

// Local framework
#include "${className}Factory.h"
#include "${intfName}Factory.h"
#include "${intfName}${LateType}.h"

// System framework
#include "CATUnicodeString.h"
#include "CATBaseDispatch.h"

// ObjectModelerBase framework
#include "CATIContainer.h"

// ObjectSpecsModeler
#include "CATISpecObject.h"

// MechModInterfaces
#include "CATIPrtPart.h"
#include "CATIAPart.h"


// Tie the implementation to its interface
// ---------------------------------------
#include "TIE_${IDLFactory}.h"
TIE_${IDLFactory}(${className}Factory);

CATImplementClass( ${className}Factory,
                   DataExtension,
                   CATBaseObject,
                   MechanicalPart );

//-----------------------------------------------------------------------------
// ${className}Factory : constructor
//-----------------------------------------------------------------------------
${className}Factory::${className}Factory(): CATBaseObject()
{
}

//-----------------------------------------------------------------------------
// ${className}Factory : destructor
//-----------------------------------------------------------------------------
${className}Factory::~${className}Factory()
{
}

//-----------------------------------------------------------------------------
// ${className}Factory : get_Application
//-----------------------------------------------------------------------------
HRESULT  ${className}Factory::get_Application(CATIAApplication *& opiApplicationOnCATIA)
{
     HRESULT rc=E_FAIL;
    opiApplicationOnCATIA=NULL;

    // Retrieve parent class
    CATBaseDispatch* piBaseOnParent=NULL;
    rc=get_Parent(piBaseOnParent);
    if (SUCCEEDED(rc) && piBaseOnParent)
    {
        rc = ( (CATIABase*) piBaseOnParent)->get_Application(opiApplicationOnCATIA);
        piBaseOnParent->Release();
        piBaseOnParent = NULL ;
    }
    return rc;
}


//-----------------------------------------------------------------------------
// ${className}Factory : get_Name
//-----------------------------------------------------------------------------
HRESULT  ${className}Factory::get_Name(CATBSTR & oName)
{
	// Alias given in .IDL file
   CATUnicodeString str ("${Prefix}Factory ");
   str.ConvertToBSTR(&oName);
   return S_OK;
}

//-----------------------------------------------------------------------------
// ${className}Factory : put_Name
//-----------------------------------------------------------------------------
HRESULT  ${className}Factory::put_Name(const CATBSTR & iName)
{
   // Method cannot be implemented in this case
   HRESULT rc = E_NOTIMPL;
   return rc;
}

//-----------------------------------------------------------------------------
// ${className}Factory : GetItem
//-----------------------------------------------------------------------------
HRESULT ${className}Factory::GetItem(const CATBSTR & iName, CATBaseDispatch *& opiBaseOnChildObject)
{
	HRESULT rc = QueryInterface(IID_${IDLFactory}, (void**) &opiBaseOnChildObject) ;

   	return rc;
}

//-----------------------------------------------------------------------------
// ${className}Factory : get_Parent
//-----------------------------------------------------------------------------
HRESULT __stdcall ${className}Factory::get_Parent(CATBaseDispatch *& opiBaseParent)
{
   // Gets the parent object - the Part feature, object which to get this factory from
   HRESULT rc=QueryInterface(IID_CATIAPart, (void**) &opiBaseParent);
   return rc;
}

//-----------------------------------------------------------------------------
// Implements ${className}Factory::AddNew${LateType}
//-----------------------------------------------------------------------------
HRESULT __stdcall ${className}Factory::Create${LateType}(
<#list Attributes as attrib>  <#t>
<#-- Check for sp_IN spec  -->
	<#if attrib.facet == "sp_IN">
		<#switch attrib.type>
			<#case "tklist_spec"><#t>
			<#case "tklist_component"><#t>
					//const CATSafeArrayVariant  * ipList${attrib.name},
			<#break>
			<#case "tk_boolean"><#t>
					boolean          ib${attrib.name} ,
			<#break>
			<#case "tk_integer"><#t>
					int          in${attrib.name} ,
			<#break>
			<#case "tk_double"><#t>
					double          id${attrib.name} ,
			<#break>
			<#case "tk_string"><#t>
					CATBSTR          iString${attrib.name} ,
			<#break>
		<#case "tk_specobject"><#t>
		<#case "tk_component"><#t>
					CATIAReference          *  ipiReference${attrib.name} ,
		</#switch>
	</#if>
</#list>
				${IDLName} *& opi${IDLName})
{
   HRESULT rc = E_FAIL;

   rc = Create${LateType}( FALSE, opi${IDLName} ) ;
   if( NULL == opi${IDLName} )
		return E_FAIL ;

<#list Attributes as attrib>  <#t>
<#-- Check for sp_IN spec  -->
<#if attrib.facet == "sp_IN">
//    rc = opi${IDLName}->put_${attrib.name}( ${attrib.name} );
</#if>
</#list>

 return rc;
}

HRESULT __stdcall ${className}Factory::Create${LateType}( boolean 		ibAddToGeomSet,
														  ${IDLName} *& opi${IDLName})
{
   HRESULT rc = E_FAIL;
   opi${IDLName}=NULL;

   CATIPrtPart_var spiPartOnThis = this ;
   if (NULL_var == spiPartOnThis)
   {
        return E_FAIL;
	}

    CATISpecObject_var spSpecOnMainTool  =  spiPartOnThis->GetMainTool();
    if ( NULL_var == spSpecOnMainTool )
	{
        return E_FAIL;
	}

    CATIContainer_var spiContainer = spSpecOnMainTool->GetFeatContainer();
    if (NULL_var == spiContainer)
	{
       return E_FAIL;
	}

	${intfName}Factory_var spFactory = spiContainer ;
    if (NULL_var == spFactory)
	{
       return E_FAIL;
	}

	CATISpecObject_var spSpec${LateType} ;
	rc = spFactory->Create${LateType}( spSpec${LateType}, ibAddToGeomSet ) ;
    if (NULL_var == spSpec${LateType})
	{
       return E_FAIL;
	}

    rc = spSpec${LateType}->QueryInterface( IID_${IDLName}, (void**) &opi${IDLName} );

   return rc;
}

